#region Using Statements
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
#endregion

namespace SkyBurner
{
    public partial class SkyCube : BaseDrawableObject
    {
        private float m_fScale;        
        private MOODModel m_skyCubeModel;
        private TextureCube m_skyCubeTexture;

        public TextureCube Texture
        {
            get { return m_skyCubeTexture; }
        }

        public SkyCube(BaseGame game, String pModel, String pTextureCube, String pShader, float fScale)
            : base(game)
        {
            m_fScale = fScale;
            m_skyCubeModel = new MOODModel(game, pModel);
            m_customEffect = game.ContentManager.Load<Effect>(pShader);
            m_skyCubeTexture = game.ContentManager.Load<TextureCube>(pTextureCube);
        }

        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime, Camera pActiveCamera)
        {
            base.Update(gameTime, pActiveCamera);

            //m_mxWorld = Matrix.CreateTranslation(pActiveCamera.Position);
            //m_mxView = Matrix.CreateLookAt(pActiveCamera.Position, pActiveCamera.LookAt, pActiveCamera.Up);
            //m_mxProjection = Matrix.CreatePerspectiveFieldOfView(pActiveCamera.FOV, pActiveCamera.AspectRatio, 1.0f, 2.0f * m_fScale);
        }

        public override void Draw(GameTime gameTime, Camera pActiveCamera)
        {
            // render all skycube faces
            m_Game.GraphicsDevice.RenderState.CullMode = CullMode.None;
            m_Game.GraphicsDevice.RenderState.DepthBufferEnable = false;
            m_Game.GraphicsDevice.RenderState.DepthBufferWriteEnable = false;
            m_Game.GraphicsDevice.RenderState.AlphaBlendEnable = false;

            m_mxWorld = Matrix.CreateTranslation(pActiveCamera.Position);
            m_mxView = Matrix.CreateLookAt(pActiveCamera.Position, pActiveCamera.LookAt, pActiveCamera.Up);
            m_mxProjection = Matrix.CreatePerspectiveFieldOfView(pActiveCamera.FOV, pActiveCamera.AspectRatio, 1.0f, 2.0f * m_fScale);

            m_customEffect.Parameters["World"].SetValue(m_mxWorld);
            m_customEffect.Parameters["View"].SetValue(m_mxView);
            m_customEffect.Parameters["Projection"].SetValue(m_mxProjection);
            m_customEffect.Parameters["Scale"].SetValue(m_fScale);
            m_customEffect.Parameters["SkyCubeTexture"].SetValue(m_skyCubeTexture);

            foreach (MOODGroup moodGroup in m_skyCubeModel.Groups)
            {
                if (moodGroup.TriangleCount <= 0)
                    continue;

                m_Game.GraphicsDevice.VertexDeclaration = moodGroup.VertexDecl;

                m_customEffect.Begin();
                foreach (EffectPass pass in m_customEffect.CurrentTechnique.Passes)
                {
                    pass.Begin();
                    m_Game.GraphicsDevice.Vertices[0].SetSource(moodGroup.VertexBuffer, 0, VertexPositionNormalTexture.SizeInBytes);
                    m_Game.GraphicsDevice.DrawPrimitives(PrimitiveType.TriangleList, 0, moodGroup.TriangleCount);
                    pass.End();
                }
                m_customEffect.End();
            }

            m_Game.GraphicsDevice.RenderState.DepthBufferEnable = true;
            m_Game.GraphicsDevice.RenderState.DepthBufferWriteEnable = true;
            m_Game.GraphicsDevice.RenderState.AlphaBlendEnable = true;

            base.Draw(gameTime, pActiveCamera);
        }
    }
}
